// 车辆事故列表
// 日期区间
laydate.render({
    elem: '#test6'
    ,range: true
  });
// 车辆选择
$('.number').on('click',()=>{
    opsearch('{:url("html/car/car")}?op=search')
 })
// 添加事故记录跳转
$('#add').on('click',()=>{
    window.location.href = "{:url('html/plus/accident_add')}"
})
var page = 1;
var pagenum = 10;
var fi=1;
var between ='{$Think.get.between}';
var carID = '{$Think.get.car_id}';
var url={
    list:api.car.accidentlist,
    delete:api.car.accidentdelete,
}
var main=[[ //表头
     {type:'checkbox', fixed: 'left'}
    ,{field: 'plate_no', title: '车牌号', fixed: 'left',templet: '#carId'}
    ,{field: 'driver_name', title: '司机'}
    ,{field: 'company_name', title: '所属公司',minWidth:215}
    ,{field: 'type', title: '类型',templet:'<div>事故</div>'}
    ,{field: 'accident_time', title: '发生时间',minWidth:165}
    ,{field:'address', title:'发生地点'}
    ,{field:'price', title:'保险赔偿金额'}
    ,{field:'handle_desc', title:'处理情况'}
    ,{field:'handle_result', title:'处理结果'}
    ,{field:'desc', title:'事故说明'}
    ,{ title: '操作',width:120, toolbar:'#demo',fixed: 'right'}
]];

var data={};
data.page=page;
data.pagenum=pagenum;
var where_json ={};
if(between){
    var start = '{$Think.get.between}'.split(' ');
	where_json.accident_time=['between time',[start[0],start[2]]]
}
   
if(carID){
    where_json.car_id=['in','{$Think.get.car_id}']
}
data.where_json=JSON.stringify(where_json)

ajaxaccident(data);
function ajaxaccident(data) {
    Load();
    //请求数据
    ajax(url.list, data, 'post').done(function (res) {
        Load(1);
        // 成功回调
        //方法级表单渲染
        console.log(res)
        table.render({
            elem: '#accident',
            cols: main,
            data: res.list,
            limit: pagenum,
            cellMinWidth: 80
        });
        if(fi==1){
            laypage.render({
                elem: 'page'
                ,count: res.count
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                ,theme: '#1E9FFF'
                ,jump: function(obj, first){
                    console.log(obj)
                    if(!first){
                        page=obj.curr;
                        data.page=page;
                        pagenum=obj.limit;
                        data.pagenum=pagenum;
                        ajaxaccident(data);
                    }else{
                        
                    }
                }
              })
              fi = 2;
        }
        table.on('tool(demo)', function(obj){
            var data1 = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var data2 = {
                accident_ids:data1.accident_id
            };
            if(layEvent === 'edit'){ 
                window.location.href = "{:url('/html/plus/accident_add')}?accident_id="+data1.accident_id;
            }else if(layEvent==='del'){
                layer.confirm('是否删除', function (index) {
                    Load()
                    ajax(url.delete, data2, 'post').done(function (res) {
                        // 成功回调
                        console.log(res)
                        layer.closeAll();
                        Msg('删除成功');
                        ajaxaccident(data)
                    }).fail(function (err) {
                        // 失败回调
                        layer.close(deleteload);
                        Err(res.msg ? res.msg : '操作失败，稍候再试');
                    });
                    layer.close(index);
                })
            }
        })
                    			//删除多个
    			$(".delete-multiple").on('click', function () {
    				var accident_ids = '';
                    var checkStatus = table.checkStatus('accident'); //test即为基础参数id对应的值
                    // console.log('多选信息')
                    // console.log(checkStatus)
    				console.log(checkStatus.data) //获取选中行的数据
    				console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
    				// console.log(checkStatus.isAll) //表格是否全选
    				for (var dei = 0; dei < checkStatus.data.length; dei++) {
    					if (dei == 0) {
    						accident_ids = checkStatus.data[dei].accident_id
    					} else {
    						accident_ids += ("," + checkStatus.data[dei].accident_id)
    					}
                    }
                    console.log('删除')
                    console.log(accident_ids)
                    var data2 ={
                        accident_ids : accident_ids
                    }
                    if(checkStatus.data.length){
                        layer.open({
                            type: 1,
                            title: false,
                            id: "delete",
                            content: '<div style="padding: 20px 100px;">是否删除</div>',
                            btn: ['确定', '取消'],
                            btnAlign: 'c' //按钮居中
                            ,
                            shade: 0 //不显示遮罩
                                ,
                            yes: function () {
                                var deleteload = layer.load(1, {
                                    shade: [0.1, '#fff'] //0.1透明度的白色背景
                                });
                                console.log(data2)
                                ajax(url.delete,data2,'post').done(function(res) {
                                    // 成功回调
                                            layer.close(deleteload);
                                            layer.closeAll();
                                            Msg('删除成功');
                                            ajaxaccident(data)
                                    }).fail(function(err) {
                                    // 失败回调
                                    layer.close(deleteload);
                                    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                    });
                            },
                            btn2: function () {
                                layer.closeAll();
                            }
                        });
                    }else{
                        Err('无选择项')
                    }

    			});
    }).fail(function (err) {
        // 失败回调
        Load(1)
    	Err('请求失败')
    });

};